//
//  FPEnterpriseRegisViewController.m
//  ForestPack
//
//  Created by jimmy on 2018/7/4.
//  Copyright © 2018年 郑洲. All rights reserved.

///  

#import "FPEnterpriseRegisViewController.h"
#import "FPSelectRoleViewController.h"
#import "Masonry.h"
#import "UIView+Utils.h"

///这个是企业认证界面
@interface FPEnterpriseRegisViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIActionSheetDelegate,UITextFieldDelegate>

@property (nonatomic,strong)  UILabel      * textLabel;
@property (nonatomic,strong)  UIImageView  * progressView;
@property (nonatomic,strong)  UIButton     * nextButton;
@property (nonatomic,strong)  UIImageView  * leftImageView ;
@property (nonatomic,strong)  UITextField  * nameTextF;

@property (nonatomic,copy)    NSString     * leftImageUrl;

@property (nonatomic,strong)  NSMutableArray  * leftImageArray;

@end

@implementation FPEnterpriseRegisViewController

- (NSMutableArray *)leftImageArray
{
    if (_leftImageArray == nil) {
        _leftImageArray = [[NSMutableArray alloc] init];
    }
    return _leftImageArray;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;
    
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setEnterpriseCertificateViewUI];
    
}

-(void)nextClick
{
    if (self.leftImageArray.count <= 0) {
        return;
    }
    
    [HttpManager uploadImageArr:self.leftImageArray success:^(id successResponse) {
        self.leftImageUrl = [NSString stringWithFormat:@"%@",successResponse[0]];
    }];
    
    if ([self.nameTextF.text isEqualToString:@""] || [self.leftImageView.image isEqual:@"营业执照"]) {
        [HttpManager showNoteMsg:@"请填写完整的信息"];
    }else{
        FPSelectRoleViewController *selectRoleVC = [[FPSelectRoleViewController alloc] init];
        selectRoleVC.enterpriseNameStr = self.nameTextF.text;
        selectRoleVC.enterpriseImageUrl = self.leftImageUrl;
        selectRoleVC.registerType = self.registerType;
        [self.navigationController pushViewController:selectRoleVC animated:true];
    }
}

-(void)backClick
{
    [self.navigationController popViewControllerAnimated:true];
}

#pragma mark - 设置上传营业执照正反面的点击事件（调相机或相册）
-(void)tapClick
{
    [self alterIconPortrait];
}

#pragma mark----- 选择弹出框
-(void)alterIconPortrait
{
    //弹出提示框
    __weak typeof(self)weakSelf = self;
    //初始化提示框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    //按钮：拍摄
    [alert addAction:[UIAlertAction actionWithTitle:@"拍摄" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf openCamare];
    }]];
    //按钮：从手机相册选择
    [alert addAction:[UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
        [weakSelf openAlbum];
    }]];
    //按钮：取消，类型：UIAlertActionStyleCancel
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}
#pragma mark - 打开相册
-(void)openAlbum
{
    //    UIImagePickerControllerSourceTypePhotoLibrary,         所有相册
    //    UIImagePickerControllerSourceTypeCamera,               相机
    //    UIImagePickerControllerSourceTypeSavedPhotosAlbum，    moment中
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    
    [self presentViewController:picker animated:YES completion:nil];
    
}

#pragma mark - 打开相机
-(void)openCamare
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.delegate = self;
    
    [self presentViewController:picker animated:YES completion:nil];
    
}


#pragma mark - <UIImagePickerControllerDelegate>
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    //选择显示的图片
    UIImage * img = info[UIImagePickerControllerOriginalImage];
    [self.leftImageArray addObject:img];
    //关闭选择界面
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

-(void)setEnterpriseCertificateViewUI
{
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(13, APP_STATUSBAR_HEIGHT, 22, 22)];
    [leftButton setImage:[UIImage imageNamed:@"返回红色"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];
    //    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    
    UIImageView *topImageView = [[UIImageView alloc] init];
    topImageView.centerX = self.view.centerX;
    topImageView.size = CGSizeMake(ScreenX375(64), ScreenX375(64));
    topImageView.left = (SCREEN_WIDTH - ScreenX375(64)) / 2;
    topImageView.top = NAVIGATION_BAR_HEIGHT + 10;
    topImageView.image = [UIImage imageNamed:@"组11"];
    [self.view addSubview:topImageView];
    
    self.textLabel = [[UILabel alloc] init];
    self.textLabel.frame = CGRectMake((SCREEN_WIDTH - ScreenX375(150))/2, topImageView.bottom, ScreenX375(150), 20);
    self.textLabel.font = [UIFont systemFontOfSize:18];
    self.textLabel.textColor = RGB_COLOR(51, 51, 51);
    self.textLabel.text = @"验证企业信息";
    self.textLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.textLabel];
    
    self.progressView = [[UIImageView alloc] init];
    self.progressView.frame = CGRectMake(30, self.textLabel.bottom + 20, SCREEN_WIDTH - ScreenX375(60), 26);
    self.progressView.contentMode = UIViewContentModeCenter;
    self.progressView.image = [UIImage imageNamed:@"完成"];
    [self.view addSubview:self.progressView];
    
    [self setCenterViewUI];
    
    self.nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.nextButton.backgroundColor = RGB_COLOR(237, 34, 59);
    self.nextButton.frame = CGRectMake(40, SCREEN_HEIGHT - 200, SCREEN_WIDTH - 80, 44);
    self.nextButton.layer.masksToBounds = true;
    self.nextButton.layer.cornerRadius = 22;
    [self.nextButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [self.nextButton setTitle:@"下一步" forState:UIControlStateNormal];
    [self.nextButton addTarget:self action:@selector(nextClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.nextButton];
}

-(void)setCenterViewUI
{
    UIView *centerView = [[UIView alloc] initWithFrame:CGRectMake(0, self.textLabel.bottom+30, SCREEN_WIDTH, 223)];
    [self.view addSubview:centerView];
    
    UIImageView *nameIamge = [[UIImageView alloc] initWithFrame:CGRectMake(40, 31, 20, 21)];
    nameIamge.image = [UIImage imageNamed:@"企业名称"];
    [centerView addSubview:nameIamge];
    
    self.nameTextF = [[UITextField alloc] initWithFrame:CGRectMake(nameIamge.right+10, 31, SCREEN_WIDTH - 100, 20)];
    self.nameTextF.placeholder = @"企业名称";
    self.nameTextF.delegate = self;
    //    [self.nameTextF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    self.nameTextF.clearButtonMode =  UITextFieldViewModeWhileEditing;
    [centerView addSubview:self.nameTextF];
    
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(40, self.nameTextF.bottom + 15, SCREEN_WIDTH - 80, 1)];
    line1.backgroundColor = RGBACOLOR(238, 238, 238, 1);
    [centerView addSubview:line1];
    
    self.leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(40, line1.bottom + 16, 140, 95)];
    self.leftImageView.userInteractionEnabled = YES;
    self.leftImageView.image = [UIImage imageNamed:@"营业执照"];
    //给UIImageView添加一个点击事件
    UIButton *tapBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 140, 95)];
    tapBtn.backgroundColor = [UIColor clearColor];
    [tapBtn addTarget:self action:@selector(tapClick) forControlEvents:UIControlEventTouchUpInside];
    [self.leftImageView addSubview:tapBtn];
    [centerView addSubview:self.leftImageView];
    
    UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, self.leftImageView.bottom + 5, 140, 12)];
    leftLabel.text = @"上传营业执照";
    leftLabel.textAlignment = NSTextAlignmentCenter;
    leftLabel.textColor = RGB_COLOR(153, 153, 153);
    leftLabel.font = [UIFont systemFontOfSize:12];
    [centerView addSubview:leftLabel];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self.nextButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:true];
}


@end

